package com.lframework.starter.web.inner.controller.system;

import com.lframework.starter.common.exceptions.impl.DefaultClientException;
import com.lframework.starter.common.utils.CollectionUtil;
import com.lframework.starter.web.core.annotations.security.HasPermission;
import com.lframework.starter.web.core.components.resp.InvokeResult;
import com.lframework.starter.web.core.components.resp.InvokeResultBuilder;
import com.lframework.starter.web.core.components.resp.PageResult;
import com.lframework.starter.web.core.controller.DefaultBaseController;
import com.lframework.starter.web.core.utils.PageResultUtil;
import com.lframework.starter.web.inner.bo.system.dept.logo.GetSysDeptLogoBo;
import com.lframework.starter.web.inner.bo.system.dept.logo.QuerySysDeptLogoBo;
import com.lframework.starter.web.inner.entity.SysDeptLogo;
import com.lframework.starter.web.inner.service.system.SysDeptLogoService;
import com.lframework.starter.web.inner.vo.system.dept.logo.CreateSysDeptLogoVo;
import com.lframework.starter.web.inner.vo.system.dept.logo.QuerySysDeptLogoVo;
import com.lframework.starter.web.inner.vo.system.dept.logo.UpdateSysDeptLogoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "部门Logo管理")
@Validated
@RestController
@RequestMapping("/system/dept/logo")
@Deprecated
public class SysDeptLogoController extends DefaultBaseController {

    @Autowired
    private SysDeptLogoService sysDeptLogoService;

    /**
     * 查询列表
     */
    @ApiOperation("查询列表")
    @HasPermission({"system:dept:logo:query"})
    @GetMapping("/query")
    public InvokeResult<PageResult<QuerySysDeptLogoBo>> query(@Valid QuerySysDeptLogoVo vo) {

        PageResult<SysDeptLogo> pageResult = sysDeptLogoService.query(getPageIndex(vo), getPageSize(vo), vo);

        List<SysDeptLogo> datas = pageResult.getDatas();
        List<QuerySysDeptLogoBo> results = null;


        if (!CollectionUtil.isEmpty(datas)) {
            results = datas.stream().map(QuerySysDeptLogoBo::new).collect(Collectors.toList());
        }

        return InvokeResultBuilder.success(PageResultUtil.rebuild(pageResult, results));
    }

    /**
     * 根据ID查询
     */
    @ApiOperation("根据ID查询")
    @ApiImplicitParam(value = "ID", name = "id", paramType = "query", required = true)
    @HasPermission({"system:dept:logo:query"})
    @GetMapping
    public InvokeResult<GetSysDeptLogoBo> get(@NotBlank(message = "ID不能为空！") String id) {
        SysDeptLogo data = sysDeptLogoService.findById(id);
        if (data == null) {
            throw new DefaultClientException("部门Logo不存在！");
        }

        GetSysDeptLogoBo result = new GetSysDeptLogoBo(data);

        return InvokeResultBuilder.success(result);
    }


    /**
     * 根据部门ID查询
     */
    @ApiOperation("根据部门ID查询")
    @ApiImplicitParam(value = "部门ID", name = "deptId", paramType = "query", required = true)
    @HasPermission({"system:dept:logo:query"})
    @GetMapping("/dept")
    public InvokeResult<List<GetSysDeptLogoBo>> getByDeptId(@NotBlank(message = "部门ID不能为空！") String deptId) {
        List<SysDeptLogo> datas = sysDeptLogoService.findByDeptId(deptId);
        List<GetSysDeptLogoBo> results = null;

        if (!CollectionUtil.isEmpty(datas)) {
            results = datas.stream().map(GetSysDeptLogoBo::new).collect(Collectors.toList());
        }
        return InvokeResultBuilder.success(results);
    }

    /**
     * 新增部门Logo
     */
    @ApiOperation("新增部门Logo")
    @HasPermission({"system:dept:logo:add"})
    @PostMapping
    public InvokeResult<Void> create(@Valid @RequestBody CreateSysDeptLogoVo vo) {
        sysDeptLogoService.create(vo);
        return InvokeResultBuilder.success();
    }

    /**
     * 修改部门Logo
     */
    @ApiOperation("修改部门Logo")
    @HasPermission({"system:dept:logo:modify"})
    @PutMapping
    public InvokeResult<Void> update(@Valid @RequestBody UpdateSysDeptLogoVo vo) {
        sysDeptLogoService.update(vo);
        sysDeptLogoService.cleanCacheByKey(vo.getId());
        return InvokeResultBuilder.success();
    }

    /**
     * 删除部门Logo
     */
    @ApiOperation("删除部门Logo")
    @ApiImplicitParam(value = "ID", name = "id", paramType = "query", required = true)
    @HasPermission({"system:dept:logo:delete"})
    @DeleteMapping
    public InvokeResult<Void> delete(@NotBlank(message = "ID不能为空！") String id) {
        sysDeptLogoService.deleteById(id);
        sysDeptLogoService.cleanCacheByKey(id);
        return InvokeResultBuilder.success();
    }

    /**
     * 根据部门ID删除Logo
     */
    @ApiOperation("根据部门ID删除Logo")
    @HasPermission({"system:dept:logo:delete"})
    @ApiImplicitParam(value = "ID", name = "id", paramType = "query", required = true)
    @DeleteMapping("/dept")
    public InvokeResult<Void> deleteByDeptId(@NotBlank(message = "部门ID不能为空！") String deptId) {
        sysDeptLogoService.deleteByDeptId(deptId);
        return InvokeResultBuilder.success();
    }
}
